package com.wydexcel.generate.process;

import com.wydexcel.generate.properties.s.ExcelCellBaseProperties;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.Map;

public class WydExcelCellBaseProcessImpl implements WydExcelCellBaseProcess {

    private Map<String, CellStyle> map = new HashMap<>();

    @Override
    public CellStyle baseProcess(Row row, Cell cell, ExcelCellBaseProperties property, String fileName, boolean isBody) {
        try {
            if (null == property) {
                String key = fileName + "_null_"+isBody;
                CellStyle cellStyle = map.get(key);
                if (null == map.get(key)) {
                    Workbook workbook = row.getSheet().getWorkbook();
                    cellStyle = workbook.createCellStyle();
                    map.put(key, cellStyle);
                    return cellStyle;
                }
                return cellStyle;
            }
            if (isBody && property.getIsHeader() == 1) {
                return null;
            }
            if (!isBody && property.getIsHeader() == 2) {
                return null;
            }
            String key = property.getFieldName() + property.getIsHeader()+isBody;
            if (null == map.get(key)) {
                Workbook workbook = row.getSheet().getWorkbook();
                CellStyle cellStyle = workbook.createCellStyle();
                if (null != property.getAlignment()) {
                    cellStyle.setAlignment((property.getAlignment()));
                }
                if (null != property.getVerticalAlignment()) {
                    cellStyle.setVerticalAlignment((property.getVerticalAlignment()));
                }
                cellStyle.setWrapText(property.isWrapTest());
                if (null != property.getBorderLeft()) {
                    cellStyle.setBorderLeft((property.getBorderLeft()));
                }
                if (null != property.getBorderRight()) {
                    cellStyle.setBorderRight((property.getBorderRight()));
                }
                if (null != property.getBorderTop()) {
                    cellStyle.setBorderTop(property.getBorderTop());
                }
                if (null != property.getBorderLeft()) {
                    cellStyle.setBorderBottom((property.getBorderLeft()));
                }
                if (null != property.getBottomBorderColor()) {
                    cellStyle.setBottomBorderColor(property.getBottomBorderColor());
                }
                if (null != property.getTopBorderColor()) {
                    cellStyle.setBottomBorderColor(property.getTopBorderColor());
                }
                if (null != property.getLeftBorderColor()) {
                    cellStyle.setLeftBorderColor(property.getLeftBorderColor());
                }
                if (null != property.getRightBorderColor()) {
                    cellStyle.setRightBorderColor(property.getRightBorderColor());
                }

                if (null != property.getFillBackgroundColor()) {
                    cellStyle.setFillPattern(FillPatternType.NO_FILL);
                    cellStyle.setFillBackgroundColor(property.getFillBackgroundColor());
                }
                if (null != property.getFillPattern()) {
                    cellStyle.setFillPattern(property.getFillPattern());
                }
                if (null != property.getFillForeGroundColor()) {
                    cellStyle.setFillForegroundColor(property.getFillForeGroundColor());
                }
                if (null != property.getFillForeGroundColor()) {
                    cellStyle.setFillForegroundColor(property.getFillForeGroundColor());
                }
                if (null != property.getIndention()) {
                    cellStyle.setIndention(property.getIndention());
                }
                if (null != property.getRotation()) {
                    cellStyle.setRotation(property.getRotation());
                }
                if (null != property.getQuotePrefixed()) {
                    cellStyle.setQuotePrefixed(property.getQuotePrefixed());
                }
                if (null != property.getHidden()) {
                    cellStyle.setHidden(property.getHidden());
                }
                if (null != property.getShrinkToFit()) {
                    cellStyle.setShrinkToFit(property.getShrinkToFit());
                }
                cellStyle.setLocked(property.isLocked());
                map.put(key,cellStyle);
                return cellStyle;
            }
            return map.get(key);
        } catch (Exception e) {
            return null;
        }
    }
}
